package pointers

import (
	"errors"
	"fmt"
)

// Bitcoin is the wallet type
// 允许从现有的类型创建新的类型
type Bitcoin int32

// Stringer is the interface from fmt
type Stringer interface {
	String() string
}

func (b Bitcoin) String() string {
	return fmt.Sprintf("%d BTC", b)
}

// Wallet is a wallet
type Wallet struct {
	// 如果一个符号（例如变量、类型、函数等）是以小写符号开头，那么它在 定义它的包之外 就是私有的
	balance Bitcoin
}

// Deposit add a dep
func (w *Wallet) Deposit(amount Bitcoin) {
	fmt.Println("address of balance in Deposit is", &w.balance)
	w.balance += amount
}

// Balance return the balance
func (w *Wallet) Balance() Bitcoin {
	return w.balance
}

// ErrorInsufficientFunds is used for withdrawing checking
var ErrorInsufficientFunds = errors.New("cannot withdraw, insufficient funds")

// Withdraw the balance
func (w *Wallet) Withdraw(amount Bitcoin) error {
	if amount > w.balance {
		return ErrorInsufficientFunds
	}
	w.balance -= amount
	return nil
}
